import 'dart:async';
import 'dart:io';

mixin FileBytesSaver {
  Duration get saveTimeout => Duration.zero;
  Timer? _saveTask;
  List<int> getSaveBytes();
  File get saveBytesFile;
  Future<void> saveBytesToFile() async {
    _saveTask?.cancel();
    _saveTask = Timer(saveTimeout, () {
      final bytes = getSaveBytes();
      final file = saveBytesFile..createSync(recursive: true);
      print('Save bytes to file: ${file.path}');
      file.writeAsBytesSync(bytes);
    });
  }
}

mixin FileStringSaver {
  Duration get saveTimeout => Duration.zero;
  Timer? _saveTask;
  String getSaveString();
  File get saveStringFile;
  Future<void> saveStringToFile() async {
    _saveTask?.cancel();
    _saveTask = Timer(saveTimeout, () {
      final string = getSaveString();
      final file = saveStringFile..createSync(recursive: true);
      print('Save string to file: ${file.path}');
      file.writeAsStringSync(string);
    });
  }
}
